﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmQLCongTy : Form
    {
        private ChiNhanhCongTyMgt chinhanhcongtyMgt = new ChiNhanhCongTyMgt();
        private KhoManagement khoMgt = new KhoManagement();
        private bool IsEdited = false;

        #region Xu ly Load
        public FrmQLCongTy()
        {
            InitializeComponent();
        }

        private void FrmQLCongTy_Load(object sender, EventArgs e)
        {
            groupBox1.Enabled = false;
            ClearTextBox();
            lblID.Hide();
            BindingToGrid();
            btnXoa.Enabled = false;
            btnUpdate.Enabled = false;
            btnLuu.Enabled = false;
            dgvQLCongTy.AutoGenerateColumns = false;
        }

        public void BindingToGrid()
        {
            ResultObj result = new ResultObj();
            result = chinhanhcongtyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> lstChiNhanhCongTy = (List<ChiNhanhCongTy>)result.Result;
            dgvQLCongTy.DataSource = lstChiNhanhCongTy;
        }
        #endregion

        #region Xu ly cac Button
        private void btnCreate_Click(object sender, EventArgs e)
        {
            ClearTextBox();
            groupBox1.Enabled = true;
            ChangeStatusTextBox(false);
            IsEdited = false;
            btnLuu.Enabled = true;
            btnUpdate.Enabled = false;
            btnXoa.Enabled = false;
            txtTenChiNhanh.Select();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtTenChiNhanh.Text == "" || txtSoDienThoai.Text == "" || txtDiaChi.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin !", "Thông báo");
            }
            else
            {
                ResultObj result = new ResultObj();

                string sTen = txtTenChiNhanh.Text;
                string sDiaChi = txtDiaChi.Text;
                string sDienThoai = txtSoDienThoai.Text;

                ChiNhanhCongTy chinhanhcongty = new ChiNhanhCongTy();
                chinhanhcongty.TenChiNhanh = sTen;
                chinhanhcongty.DiaChiChiNhanh = sDiaChi;
                chinhanhcongty.SoDienThoai = sDienThoai;

                if (!IsEdited)
                {
                    result = chinhanhcongtyMgt.SaveNewChiNhanhCongTy(chinhanhcongty);
                    chinhanhcongty.Id = result.ReturnCode;
                }
                else
                {
                    chinhanhcongty.Id = Convert.ToInt32(lblID.Text);
                    result = chinhanhcongtyMgt.UpdateChiNhanhCongTy(chinhanhcongty);
                    IsEdited = false;
                }

                if (result.ReturnCode > 0)
                {
                    MessageBox.Show(" Lưu thành công", "Thông báo");
                    FrmQLCongTy_Load(null, null);
                }
                else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                {
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                    txtTenChiNhanh.Select();
                }
                else
                {
                    MessageBox.Show("Thất bại");
                    txtTenChiNhanh.Select();
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            IsEdited = true;
            groupBox1.Enabled = true;
            ChangeStatusTextBox(false);
            btnLuu.Enabled = true;
            btnUpdate.Enabled = false;
            btnXoa.Enabled = false;
            txtTenChiNhanh.Select();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = MessageBox.Show("Bạn có chắc chắn muốn xóa chi nhánh công ty này không?", "Xóa chi nhánh công ty", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                ResultObj resultObj = new ResultObj();

                string sTen = txtTenChiNhanh.Text;
                string sDiaChi = txtDiaChi.Text;
                string sDienThoai = txtSoDienThoai.Text;

                ChiNhanhCongTy chinhanhcongty = new ChiNhanhCongTy();
                chinhanhcongty.TenChiNhanh = sTen;
                chinhanhcongty.DiaChiChiNhanh = sDiaChi;
                chinhanhcongty.SoDienThoai = sDienThoai;
                chinhanhcongty.Id = Convert.ToInt32(lblID.Text);
                chinhanhcongty.IsDelete = 1;

                resultObj = chinhanhcongtyMgt.UpdateChiNhanhCongTy(chinhanhcongty);
                FrmQLCongTy_Load(null, null);
            }
        }
        #endregion

        #region Xu ly cac su kien
        public void ChangeStatusTextBox(bool IsReadOnly)
        {
            if (IsReadOnly)
            {
                txtTenChiNhanh.ReadOnly = true;
                txtDiaChi.ReadOnly = true;
                txtSoDienThoai.ReadOnly = true;
            }
            else
            {
                txtTenChiNhanh.ReadOnly = false;
                txtDiaChi.ReadOnly = false;
                txtSoDienThoai.ReadOnly = false;
            }
        }

        public void ClearTextBox()
        {
            txtTenChiNhanh.Clear();
            txtDiaChi.Clear();
            txtSoDienThoai.Clear();
        }

        private void dgvQLCongTy_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                ChangeStatusTextBox(true);
                btnUpdate.Enabled = true;
                btnLuu.Enabled = false;
                btnXoa.Enabled = true;
                lblID.Text = dgvQLCongTy.Rows[e.RowIndex].Cells[0].Value.ToString();
                txtTenChiNhanh.Text = dgvQLCongTy.Rows[e.RowIndex].Cells[1].Value.ToString();
                txtDiaChi.Text = dgvQLCongTy.Rows[e.RowIndex].Cells[2].Value.ToString();
                txtSoDienThoai.Text = dgvQLCongTy.Rows[e.RowIndex].Cells[3].Value.ToString();
            }
        }

        private void txtSoDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Vui lòng nhập số !");
                e.Handled = true;
            }
        }
        #endregion
    }
}
